/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-12-12 下午1:55 Created by dwzhang
 */
package com.jade.bss.base.customer.impl;

import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.bss.base.customer.CustomerAddress;
import com.jade.bss.base.customer.CustomerAddressDao;
import com.jade.bss.base.customer.CustomerException;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;

/**
 * 采用JPA方式实现的地址数据访问接口。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-12-12 下午1:55
 */
@Named ("bss_customerAddressDao")
public class JpaAddressDao
        implements CustomerAddressDao
{
    private static final String SQL = "SELECT customerAddress FROM com.jade.bss.base.customer.CustomerAddress customerAddress";
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    public void addAddress(CustomerAddress address)
            throws CustomerException
    {
        entityManager.persist(address);
        if(address.isDefault())
            setDefaultAddress(address.getCustomerId(),address.getType(),address.getId());
    }

    public void updateAddress(CustomerAddress address)
            throws CustomerException
    {
        entityManager.merge(address);
    }

    public void setDefaultAddress(long customerId, String type, long id)
            throws CustomerException
    {
        String sql1 = "UPDATE CustomerAddress SET isDefault=:isDefault WHERE id=:id";
        String sql2 = "UPDATE CustomerAddress SET isDefault=false WHERE customerId=:customerId AND type=:type AND isDefault=true";
        Query query = entityManager.createQuery(sql2);
        query.setParameter("customerId", customerId);
        query.setParameter("type", type);
        query.executeUpdate();
        query = entityManager.createQuery(sql1);
        query.setParameter("isDefault", true);
        query.setParameter("id", id);
        query.executeUpdate();
    }

    public void removeAddress(CustomerAddress address)
            throws CustomerException
    {
        entityManager.remove(address);
    }

    public CustomerAddress getAddress(long id)
    {
        return entityManager.find(CustomerAddress.class, id);
    }

    public List<CustomerAddress> listAddress(long customerId, Criteria criteria)
    {
        criteria.add(Restrictions.eq("customerId", customerId));
        return SqlUtils.sqlQueryWithoutCount(SQL, entityManager, criteria);
    }

    public PagingList<CustomerAddress> queryAddress(long customerId, Criteria criteria)
    {
        criteria.add(Restrictions.eq("customerId", customerId));
        return SqlUtils.sqlQuery(SQL, entityManager, criteria);
    }

    public void moveAddress(long fromCustomerId, long toCustomerId)
            throws CustomerException
    {
        String sql = "UPDATE CustomerAddress SET customerId=:customerId WHERE customerId=:oldCustomerId";
        Query query = entityManager.createQuery(sql);
        query.setParameter("customerId", toCustomerId);
        query.setParameter("oldCustomerId", fromCustomerId);
        query.executeUpdate();
    }

    public PagingList<CustomerAddress> queryAddress(Criteria criteria)
    {
        return SqlUtils.sqlQuery(SQL, entityManager, criteria);
    }
}
